package team.seekers.catering.common.utils;

import team.seekers.catering.food.entity.pojo.ContainFoodListDTO;
import team.seekers.catering.food.entity.pojo.ContainNutrimentListDO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ConvertUtil {

    public static List<ContainFoodListDTO> contentToContentList(byte[] content){
        //将取出的二进制数据反序列化成序列化的对象，依旧使用对象流处理
        if(content == null){
            return new ArrayList<>();
        }
        ByteArrayInputStream byteInt=new ByteArrayInputStream(content);
        ObjectInputStream objInt= null;
        try {
            objInt = new ObjectInputStream(byteInt);
            List<ContainFoodListDTO> o=(List<ContainFoodListDTO>)objInt.readObject();
            return o;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<ContainNutrimentListDO> ndToNutrimentList(byte[] content){
        //将取出的二进制数据反序列化成序列化的对象，依旧使用对象流处理
        if(content == null){
            return new ArrayList<ContainNutrimentListDO>();
        }
        ByteArrayInputStream byteInt=new ByteArrayInputStream(content);
        ObjectInputStream objInt= null;
        try {
            objInt = new ObjectInputStream(byteInt);
            List<ContainNutrimentListDO> o=(List<ContainNutrimentListDO>)objInt.readObject();
            return o;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] contentFromContentList(List<ContainFoodListDTO> foodContentList){
        ByteArrayOutputStream byt=new ByteArrayOutputStream();
        ObjectOutputStream obj= null;
        try {
            obj = new ObjectOutputStream(byt);
            obj.writeObject(foodContentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byt.toByteArray();
    }

    public static byte[] ndFromContentList(List<ContainNutrimentListDO> nutrimentList){
        ByteArrayOutputStream byt=new ByteArrayOutputStream();
        ObjectOutputStream obj= null;
        try {
            obj = new ObjectOutputStream(byt);
            obj.writeObject(nutrimentList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byt.toByteArray();
    }
}
